<!--
 作者：ex_wangshuai
 日期：2016-11-07
 功能：首页-掌天下-单品牌数据概览
 参数：
 事件：
-->
<template>
  <div class="singleDataOviews">
    <div class="loading-box">
      <img src="../../../../static/img/loading.gif" class="loading">
    </div>
    <toolbar :view-title="viewTitle">
      <div class="leftButton" type="button" @click="this.$broadcast('goBack')" slot="left-button">&nbsp;</div>
      <div class="rightButton"
           type="button"
           slot='right-button'>
        <div class="filterButton" @click="showFilter" v-show="!filterOpenState">
          <span class="filterIcon" :page-id="routeKey">&nbsp;</span>
          <span>筛选</span>
        </div>
        <div class="menuButton" @click="showSlideMenu" v-show="!filterOpenState">
          <span class="menuIcon">&nbsp;</span>
          <span>菜单</span>
        </div>
        <span v-show="filterOpenState" @click="doFilter" class="filterOver">完成</span>
      </div>
    </toolbar>
    <div class="store-ranking-content">
      <div class="top-box">
        <div class="title-time">
          <p class="title">{{tableTitle.subTitle}}</p>
        </div>
        <div class="filter-message-box clearfix">
          <a class="filter-condition" @click="showFilter('pinlei')" href="javascript:;">{{pinlei}}</a>
          <a class="filter-condition" href="javascript:;" @click="showFilter('pinpai')">{{pinpai}}</a>
          <a class="filter-condition" @click="showFilter('pingtai')" href="javascript:;">{{pingtai}}</a>
          <a class="time filter-condition" @click="showFilter('shijian')" href="javascript:;">{{tableTitle.starttime | timeFilter}} - {{tableTitle.endtime | timeFilter}}</a>
        </div>
      </div>
      <div style="height: 1.5vh;background:#eeeeee;"></div>
      <div class="importantMessage">
        <div class="important">
          *销额排名、销量排名均表示已选品牌在目标品类内排名
        </div>
        <div class="important">
          销额占比、销量占比均表示目标品类在已选品牌内占比
        </div>
      </div>
      <table-locked :table-rows="tableData" :grid-columns="gridColumns" :thead="thead" :height="height"
                    :single-data="singleData">
      </table-locked>
      <!--<div class="item-data" v-if="!tableData.length">-->
        <!--暂无数据-->
      <!--</div>-->
    </div>
    <page-turning :mkey="routeKey" :page-id="'industry'">
    </page-turning>
    <slide-list v-if="showList">
    </slide-list>
    <filter-view v-show="filterOpenState"
                 :default-org-id="defaultOrgId"
                 :default-category="defaultCategory"
                 :default-category-id="defaultCategoryId"
                 :choose-platforms= "pingtai"
                 :choose-platforms-id= "pingtaiId"
                 :page-id="routeKey"
                 :filter-button = "filterButton"
                 :default-start-date="startTime"
                 :default-end-date="endTime">
    </filter-view>
    <confirm :show.sync="timeoutStatus"
             title="加载失败,您可以"
             cancel-text="返回首页"
             confirm-text="重新加载"
             @on-cancel="this.$route.router.go('/home')"
             @on-confirm="reload">
    </confirm>
    <confirm :show.sync="show" title="是否退出？" cancel-text="取消" confirm-text="确定" @on-confirm="onAction('确认')"></confirm>
  </div>
</template>

<script>
  import Toolbar from '../../common/Toolbar.vue'
  import TableLocked from '../../common/TableLocked.vue'
  import pageTurning from '../../common/pageTurning.vue'
  import slideList from '../list/slideList.vue'
  import FilterView from '../../common/FilterView.vue'
  import Confirm from 'vux/dist/components/Confirm'

  export default{
    components: {
      Toolbar,
      TableLocked,
      pageTurning,
      slideList,
      FilterView,
      Confirm
    },
    data () {
      var data = {}
      data.show = false
      data.timeoutStatus = false
      data.showList = false
      data.pinlei = '所有品类'
      data.pinpai = ''
      data.pingtai = '所有平台'
      data.pingtaiId = ''
      data.startTime = ''
      data.endTime = ''
      data.filterButton = ''
      data.defaultOrgId = ''
      data.defaultCategory = ''
      data.defaultCategoryId = ''
      data.routeKey = ''
      data.filterOpenState = false
      data.viewTitle = ''
      data.singleData = true
      data.height = '90vh'
      data.tableTitle = {}
      data.tableData = []
      data.gridColumns = ['category', 'avg_price', 'sales_ranking', 'sales_amount_ranking', 'sales', 'qoq_sales_percentage', 'sales_proportion', 'sales_amount', 'qoq_sales_amount_percentage', 'sales_amount_proportion', 'score_nums', 'score_goods_percentage']
      data.thead = ['品类', '均价', '销量排名', '销额排名', '销量', '销量环比', '销量占比', '销额(万元)', '销额环比', '销额占比', '总评论数', '好评率']
      data.params = {
        mkey: '',
//        refresh: true,
        org_code: '',
        brand_id: '',
        brand: '',
        category_id: '',
        category: '',
        platform_id: '',
        platform: '',
        dateSeg: '',
        startTime: '',
        endTime: ''
      }
      return data
    },
    methods: {
      reload: function () {
        window.location.reload()
      },
      goBack: function () {
//        window.history.back()
        this.show = true
      },
      onAction: function (value) {
        if (value === '确认') {
          window.cordova.exec(function (success) {
          }, function (error) {
            window.alert('Error: ' + error)
          }, 'MideaCommon', 'exit', [])
        }
      },
      doFilter: function () {
        this.$broadcast('doFilter')
      },
      showFilter: function (value) {
        this.filterButton = value
        this.filterOpenState = true
      },
      showSlideMenu: function () {
        this.showList = !this.showList
        var self = this
        setTimeout(function () {
          self.$broadcast('slideStart')
        }, 0)
      },
      getHttpData: function (httpUrl, params) {
        this.$http.post(httpUrl, params, {_timeout: 30000}).then((res) => {
          if (res.json().error) {
            this.timeoutStatus = true
            return
          }
          this.pinpai = res.json().brand
//        this.$http.get('http://testbigdataservice.midea.com/midea-report-mobile/static/' + this.routeKey + '.json', {}).then(function (res) {
          document.getElementsByClassName('loading-box')[0].style.display = 'none'
          var data = res.json()
          this.pinlei = data.categoryCnd || data.category
          this.pinpai = data.brandCnd || data.brand
          this.pingtai = data.platformCnd || data.platform
          this.pingtaiId = data.platform_id || ''
          this.defaultOrgId = data.org_code || ''
          this.defaultCategory = data.category || ''
          this.defaultCategoryId = data.category_id || ''
          this.startTime = data.startTime
          this.endTime = data.endTime
          // 页面标题
          this.viewTitle = res.json().title
          this.tableTitle = res.json().chartDatas[0]
          this.tableData = res.json().chartDatas[0].detail
          this.thead = ['品类', '均价', '销量排名', '销额排名', '销量' + res.json().chartDatas[0].saleUnit, '销量环比', '销量占比', '销额(万元)', '销额环比', '销额占比', '总评论数', '好评率']
          this.$broadcast('loadDataDone')
          setTimeout(function () {
            var htmlHeight = document.documentElement.clientHeight
            var toorBarHeight = document.getElementsByClassName('toolbar')[0].clientHeight
            var topBoxHeight = document.getElementsByClassName('top-box')[0].clientHeight
            var tapHeight = document.getElementsByClassName('tab')[0].clientHeight
            var messageHeight = document.getElementsByClassName('importantMessage')[0].clientHeight
            var divBox = document.getElementById('divBox1')
            divBox.style.maxHeight = htmlHeight - toorBarHeight - topBoxHeight - tapHeight - messageHeight - 0.015 * htmlHeight - 2 + 'px'
          }, 0)
        }, (res) => {
          this.timeoutStatus = true
        })
      }
    },
    events: {
      'filterConditions': function (filterConditions) {
        if (filterConditions.category !== '') {
          this.pinlei = filterConditions.category
          this.pinlei = this.pinlei.replace(/,/g, '，')
        }
        if (filterConditions.platform !== '') {
          this.pingtai = filterConditions.platform
        }
        if (filterConditions.brand !== '') {
          this.pinpai = filterConditions.brand
          this.pingtai = this.pingtai.replace(/,/g, '，')
        }
        document.getElementsByClassName('loading-box')[0].style.display = 'block'
        this.params.org_code = filterConditions.org_code || ''
        this.params.brand_id = filterConditions.brand_id
        this.params.brand = filterConditions.brand
        this.params.category = filterConditions.category
        this.params.category_id = filterConditions.category_id
        this.params.platform = filterConditions.platform
        this.params.platform_id = filterConditions.platform_id
        this.params.dateSeg = filterConditions.dateSeg
        this.params.startTime = filterConditions.starttime
        this.params.endTime = filterConditions.endtime
        this.params.mkey = this.routeKey
        this.getHttpData(this.httpUrl, this.params)
      }
    },
    created () {
      this.routeKey = 'singleDataOviews'
      this.httpUrl = window.interface.indusPage
      var params = this.params
      params.mkey = this.routeKey
      this.getHttpData(this.httpUrl, params)
    },
    filters: {
      timeFilter: function (val) {
        if (val) {
          val = val.split('-')
          val = val.join('')
          return val
        }
      }
    },
    ready: function () {
    }
  }
</script>

<style lang="scss" scoped>
  .store-ranking-content {
    position: relative;
    margin-top: 6.6vh;
  }

  .top-box{
    padding: 2.2vh 0 2.2vh .3rem;
    border: 1px solid rgb(238, 238, 238)
  }
  .filter-message-box{
    font-size: 0.28rem;
  }

  .filter-condition{
    margin-right: 0.2rem;
    margin-top: 1.1vh;
    font-size: 0.24rem;
    float: left;
    color: #4A90E2;
    padding: 0.35vh 1.2vh;
    box-sizing: border-box;
    border: 1px solid #4A90E2;
    background: #FAFCFF;
    border-radius: 3px;
    max-width: 85%;
    white-space:nowrap;
    overflow:hidden;
    text-overflow:ellipsis;
  }
  .title {
    background: url('../../../../static/img/time-title.png') no-repeat center right;
    background-position: 0 center;
    background-size: .28rem .26rem;
    padding-left: .4rem;
    color: #333333;
  }

  .important0 {
    font-size: 0.28rem;
    padding-left: 0.3rem;
    margin-top: 0.22rem;
  }

  .important1 {
    font-size: 0.24rem;
    padding-left: 0.3rem;
    margin: 0.12rem 0 0.22rem 0;
  }

  .btnPage {
    position: fixed;
    left: 0;
    top: 50vh;
    width: 100%;
    z-index: 200;
  }

  .btn-previous {
    width: 0.4rem;
    height: 0.8rem;
    float: left;
    background: rgba(0, 0, 0, 0.2);
  }

  .btn-next {
    width: 0.4rem;
    height: 0.8rem;
    float: right;
    background: rgba(0, 0, 0, 0.2);
  }

  .leftButton {
    width: .5rem;
    height: 100%;
    background: url('../../../../static/img/btn-back.png') no-repeat center right;
    background-size: 100%;
  }
  .importantMessage{
    padding: 0.9vh 0 1vh 0.3rem;
  }
  .important {
    font-size: 0.24rem;
    color: #666666;
  }
  .importantMessage div:nth-child(2){
    padding-left: 1.4%;
  }
</style>
